package cn.myword.wordTemple.controller;



import cn.hutool.json.JSONUtil;
import cn.myword.wordTemple.entity.Table;
import cn.myword.wordTemple.tools.UrlHandle;
import cn.myword.wordTemple.util.MyWordTemplateEngineUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.velocity.VelocityContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.myword.wordTemple.entity.ReciveData;
import cn.myword.wordTemple.entity.RequestWord;
import cn.myword.wordTemple.mapper.TableMapper;

import javax.annotation.Resource;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;

import static cn.hutool.core.text.CharSequenceUtil.toCamelCase;


/**
 *
 * @author psz
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/get")
public class CoreController {


    @Resource
    private TableMapper goodMapper;
    @PostMapping("/word")
    ResponseEntity get(@RequestBody RequestWord requestWord){
        ReciveData reciveData=new ReciveData();
        reciveData.setUrls(UrlHandle.getUrls(toCamelCase(requestWord.getRealTableName())));
        List<Table> tables = goodMapper.sel(requestWord.getRealTableName());
        HashMap<String,Table> hashMap=new HashMap<>();
        for (Table table : tables) {
            hashMap.put(table.get参数名(),table);
        }
        reciveData.setTables(tables);
        List<Table> tableListPage=new ArrayList<>();
        for (String e: requestWord.getPageListParam()){
            tableListPage.add(hashMap.get(toCamelCase(e)));
        }
        reciveData.setPageTables(tableListPage);
        reciveData.setIdsDes(requestWord.getIdsDes());
        //System.out.println(JSONObject.toJSONString(tables));
        try {
            System.out.println(JSONUtil.toJsonStr(reciveData));
            Map<String, Object> data = new LinkedHashMap<>();
            MyWordTemplateEngineUtil.setParam(data,reciveData);
            System.out.println(JSONUtil.toJsonStr(data));
            JSONObject jsonObject= MyWordTemplateEngineUtil.generator( Paths.get(System.getProperty("user.dir"),
                    "model").toString()+"/mydemo.docx", new VelocityContext(data));
            File file = (File) jsonObject.get("file");
            // 创建 FileSystemResource
            FileSystemResource resource = new FileSystemResource(file);
            // 设置响应头部信息
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
            // 返回文件
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
        return null;
    }
}
